home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter16 / isohex16_3 / isomousemap.cpp < prev    next >
C/C++ Source or Header  |  2000-08-07  |  5KB  |  279 lines

  1. //IsoMouseMap.cpp
  2. #include "IsoMouseMap.h"
  3. #include "GDICanvas.h"
  4.  
  5. //constructor
  6. CMouseMap::CMouseMap()
  7. {
  8.     //set defaults
  9.     //scroller
  10.     SetScroller(NULL);
  11.  
  12.     //walker
  13.     SetTileWalker(NULL);
  14.  
  15.     //initialize mousemap to zeros
  16.     mmdLookUp=NULL;
  17.     iWidth=0;
  18.     iHeight=0;
  19.  
  20.     //mousemap
  21.     Create(1,1);
  22.  
  23.     //reference point
  24.     ptRef.x=0;
  25.     ptRef.y=0;
  26. }
  27.  
  28. //destructor
  29. CMouseMap::~CMouseMap()
  30. {
  31.     //destroy the lookup table
  32.     Destroy();
  33. }
  34.  
  35. //load mousemap
  36. void CMouseMap::Load(LPCTSTR lpszFileName)
  37. {
  38.     //gdi canvas
  39.     CGDICanvas gdic;
  40.  
  41.     //load in graphic
  42.     gdic.Load(NULL,lpszFileName);
  43.  
  44.     //create mousemap
  45.     Create(gdic.GetWidth(),gdic.GetHeight());
  46.  
  47.     //grab corner points
  48.     COLORREF crNE=GetPixel(gdic,iWidth-1,0);
  49.     COLORREF crNW=GetPixel(gdic,0,0);
  50.     COLORREF crSE=GetPixel(gdic,iWidth-1,iHeight-1);
  51.     COLORREF crSW=GetPixel(gdic,0,iHeight-1);
  52.  
  53.     //test point
  54.     COLORREF crTest;
  55.  
  56.     //fill in lookup
  57.     for(int x=0;x<iWidth;x++)
  58.     {
  59.         for(int y=0;y<iHeight;y++)
  60.         {
  61.             //grab pixel
  62.             crTest=GetPixel(gdic,x,y);
  63.  
  64.             //check against corner points
  65.             if(crTest==crNE) mmdLookUp[x+y*iWidth]=MM_NE;
  66.             if(crTest==crSE) mmdLookUp[x+y*iWidth]=MM_SE;
  67.             if(crTest==crNW) mmdLookUp[x+y*iWidth]=MM_NW;
  68.             if(crTest==crSW) mmdLookUp[x+y*iWidth]=MM_SW;
  69.         }
  70.     }
  71. }
  72.  
  73. //create a blank mousemap
  74. void CMouseMap::Create(int iWidth,int iHeight)
  75. {
  76.     //destroy any old lookup table
  77.     Destroy();
  78.  
  79.     //copy size
  80.     this->iWidth=iWidth;
  81.     this->iHeight=iHeight;
  82.  
  83.     //allocate buffer
  84.     mmdLookUp=new MOUSEMAPDIRECTION[iWidth*iHeight];
  85.  
  86.     //clear out buffer
  87.     for(int count=0;count<iWidth*iHeight;count++)
  88.     {
  89.         mmdLookUp[count]=MM_CENTER;
  90.     }
  91. }
  92.  
  93. //destroy mousemap
  94. void CMouseMap::Destroy()
  95. {
  96.     //if allocated, deallocate
  97.     if(mmdLookUp)
  98.     {
  99.         delete[] mmdLookUp;
  100.         iWidth=0;
  101.         iHeight=0;
  102.     }
  103. }
  104.  
  105. //width/height
  106. int CMouseMap::GetWidth()
  107. {
  108.     //return width
  109.     return(iWidth);
  110. }
  111.  
  112. int CMouseMap::GetHeight()
  113. {
  114.     //return height
  115.     return(iHeight);
  116. }
  117.  
  118. //reference
  119. //get the reference point
  120. POINT* CMouseMap::GetReferencePoint()
  121. {
  122.     //return point
  123.     return(&ptRef);
  124. }
  125.  
  126. //set the reference point
  127. void CMouseMap::SetReferencePoint(POINT* pptRefPt)
  128. {
  129.     //set new reference point
  130.     ptRef.x=pptRefPt->x;
  131.     ptRef.y=pptRefPt->y;
  132. }
  133.  
  134. //calculate the reference point based on a tile plotter and an extent rect
  135. void CMouseMap::CalcReferencePoint(CTilePlotter* pTilePlotter,RECT* prcExtent)
  136. {
  137.     //map point
  138.     POINT ptMap;
  139.     ptMap.x=0;
  140.     ptMap.y=0;
  141.  
  142.     //plotted point
  143.     ptRef=pTilePlotter->PlotTile(ptMap);
  144.  
  145.     //adjust ptRef by left and top of extent rect
  146.     ptRef.x+=prcExtent->left;
  147.     ptRef.y+=prcExtent->top;
  148. }
  149.  
  150. //map the mouse
  151. POINT CMouseMap::MapMouse(POINT ptMouse)
  152. {
  153.     //convert to world coordinates from screen coordinates
  154.     //check for a scroller. if there is no scroller, then assume screen coordinates are the same as world coordinates
  155.     if(pScroller)
  156.     {
  157.         //convert from screen to world
  158.         ptMouse=pScroller->ScreenToWorld(ptMouse);
  159.     }
  160.  
  161.     //subtract reference point
  162.     ptMouse.x-=ptRef.x;
  163.     ptMouse.y-=ptRef.y;
  164.  
  165.     //convert to mousemap coordinates
  166.     POINT ptMouseMapCoarse;
  167.     POINT ptMouseMapFine;
  168.  
  169.     //coarse coordinates
  170.     ptMouseMapCoarse.x=ptMouse.x/GetWidth();
  171.     ptMouseMapCoarse.y=ptMouse.y/GetHeight();
  172.  
  173.     //fine coordinates
  174.     ptMouseMapFine.x=ptMouse.x%GetWidth();
  175.     ptMouseMapFine.y=ptMouse.y%GetHeight();
  176.  
  177.     //adjust for negative fine coordinates
  178.     if(ptMouseMapFine.x<0)
  179.     {
  180.         ptMouseMapFine.x+=iWidth;
  181.         ptMouseMapCoarse.x--;
  182.     }
  183.     if(ptMouseMapFine.y<0)
  184.     {
  185.         ptMouseMapFine.y+=iHeight;
  186.         ptMouseMapCoarse.y--;
  187.     }
  188.  
  189.     //check for a tile walker. if there is no tile walker, convert from coarse coordinates directly into map coordinates
  190.     POINT ptMap;
  191.     ptMap.x=0;
  192.     ptMap.y=0;
  193.     if(pTileWalker)
  194.     {
  195.         //coarse tilewalk
  196.         //north
  197.         while(ptMouseMapCoarse.y<0)
  198.         {
  199.             ptMap=pTileWalker->TileWalk(ptMap,ISO_NORTH);
  200.             ptMouseMapCoarse.y++;
  201.         }
  202.         //east
  203.         while(ptMouseMapCoarse.x>0)
  204.         {
  205.             ptMap=pTileWalker->TileWalk(ptMap,ISO_EAST);
  206.             ptMouseMapCoarse.x--;
  207.         }
  208.         //south
  209.         while(ptMouseMapCoarse.y>0)
  210.         {
  211.             ptMap=pTileWalker->TileWalk(ptMap,ISO_SOUTH);
  212.             ptMouseMapCoarse.y--;
  213.         }
  214.         //west
  215.         while(ptMouseMapCoarse.x<0)
  216.         {
  217.             ptMap=pTileWalker->TileWalk(ptMap,ISO_WEST);
  218.             ptMouseMapCoarse.x++;
  219.         }
  220.         //fine tilewalk
  221.         switch(mmdLookUp[ptMouseMapFine.x+ptMouseMapFine.y*GetWidth()])
  222.         {
  223.         case MM_NE:
  224.             {
  225.                 ptMap=pTileWalker->TileWalk(ptMap,ISO_NORTHEAST);
  226.             }break;
  227.         case MM_SE:
  228.             {
  229.                 ptMap=pTileWalker->TileWalk(ptMap,ISO_SOUTHEAST);
  230.             }break;
  231.         case MM_SW:
  232.             {
  233.                 ptMap=pTileWalker->TileWalk(ptMap,ISO_SOUTHWEST);
  234.             }break;
  235.         case MM_NW:
  236.             {
  237.                 ptMap=pTileWalker->TileWalk(ptMap,ISO_NORTHWEST);
  238.             }break;
  239.         }
  240.     }
  241.     else
  242.     {
  243.         //coarse coordinates are map coordinates
  244.         ptMap=ptMouseMapCoarse;
  245.     }
  246.     //return map coordinate
  247.     return(ptMap);
  248. }
  249.  
  250. //scroller
  251. //get the scroller
  252. CScroller* CMouseMap::GetScroller()
  253. {
  254.     //return the scroller
  255.     return(pScroller);
  256. }
  257.  
  258. //set the scroller
  259. void CMouseMap::SetScroller(CScroller* pScroller)
  260. {
  261.     //set the scroller
  262.     this->pScroller=pScroller;
  263. }
  264.  
  265. //walker
  266. //get the tile walker
  267. CTileWalker* CMouseMap::GetTileWalker()
  268. {
  269.     //return walker
  270.     return(pTileWalker);
  271. }
  272.  
  273. //set the tile walker
  274. void CMouseMap::SetTileWalker(CTileWalker* pTileWalker)
  275. {
  276.     //set the tile walker
  277.     this->pTileWalker=pTileWalker;
  278. }
  279.